% Compute statistics associated to the devaluation for model with free
% entry contained in Table B.6.

Rn = SPn + RXn*wn; % tradables, total sales
MFn= (1-sNn)*sMn * In; % final good imports, MF, deflated by w 
T =param.T;
disp('Check balanced trade condition after devaluation') 
disp(((Mn+MFn)/(RXn+T)-1)*100)
disp('Growth in Wage and Spending')
disp([ (wn/w-1)*100 (Seq/SP-1)*100 ])
disp('Growth in mass of firms')
disp((Neq/1-1)*100)

Pn = (PriceChange/100+1)*P;
disp('% Growth in price of domestic input bundle')
disp(PriceChange)
disp('% Growth overall consumer price index')
disp((Ptotn/ (Ptot) - 1) * 100)

% change in correlation between sales and import shares conditional on
% importing
Dcorr1= corr(log(omega1(sD1<1)),1-sD1(sD1<1))-corr(log(omega(sD<1)),1-sD(sD<1));
  
disp('Change in aggregate import share')
disp((EqMoments.Aggsi-Aggsi)*100)   
disp('Change in correlation between sales and import shares')
disp(Dcorr1)    
    
    
% Changes in distribution of import and export shares
psi1 = prctile((1-sD1(sD1<1))*100,[10 25 50 75 90 95]); % percentiles of import shares conditional on importing
dpsi =psi1-psi;
disp('Change in percentiles of import shares (importers)')
disp(dpsi([1 2 3 4 6]))

si_avg1 = mean((1-sD1(sD1<1))*100); si_avg=  mean((1-sD(sD<1))*100); dsi_avg = si_avg1-si_avg;

% BHC Decomposition (Table 9)

[With, B, ~, ~, ~, ~,~,~,~] = Decom(phi, sD, sD1,omega,omega1,sX1);
BHCdisplay = [With B];
disp('BHC Decomposition: Within, Between')
BHCdisplay = [round(BHCdisplay,2)]; 
disp(BHCdisplay)


% Change in trade balance over sales
ExpoS = TotalExports_norm*w/R; 
ExpoS1 = TotalExports_norm1*wn/Rn;
ImpoS = (TotalImports_norm-MF)*w/R; 
ImpoS1 = (TotalImports_norm1-MFn)*wn/Rn;
disp('Change in trade balance over sales')
disp(((ExpoS1-ImpoS1)-(ExpoS-ImpoS))*100)
    
cd ../../Output   

Table_B6 = [(Neq/1-1)*100;(EqMoments.Aggsi-Aggsi)*100;dsi_avg;Dcorr1;BHCdisplay(1);BHCdisplay(2)];
Table_B6 = round(Table_B6,2);

csvwrite('TableB6.csv',Table_B6);

cd ../QuantitativeModel/mfiles